/**
 * 服务预约维护 - 回访结案页面
 * @author miaoyl
 * @date 2019/04/09
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: {
                GWAK001: null,
                GWAKL004: null,
                MMRD013S: null,
                operation: null,
                MMRNDATE: null,
                MMRN008: null,
                MMRN005: null,
                MMRG102: null,
                MMRG100: null,
                MMRG100_DATA: null
            },
            templateImageUrl:[],
            dialogImageUrl: '',
            dialogVisible: false,
            imgStr_:{imgStr:''},
            img_list:[],
            imgs: g_ctxPath + 'upload/imgs',
            rules: {
                MMRD001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD019: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD020: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRDDOCNO:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                mmrd001Options:app.datas_("crm_gwai740_mmrd001"),
                mmrd005Options:app.datas_("crm_gwai740_mmrd005"),
                mmrd019Options:app.datas_("crm_gwai740_mmrd019"),
                mmrd020Options:app.datas_("crm_gwai740_mmrd020"),
                mmrd023Options:app.datas_("crm_gwai740_mmrd023"),
                mmrd013Options:[]
            },
        },
        cols = [[
            {field: 'MMRJL004',width: 160, title: '指标名称'}
            , {field: 'MMRJ003', width: 160, title: '指标标准分值'}
            , {field: 'MMRN004', width: 80, title: '得分',edit:'text'}
            , {field: 'MMRJ001', width: 80, title: '指标编号',hide:true}
        ]],
        table,
        gwai740EditTable,
        callObj
    ;

    var Gwai740Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai740_huifang',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }
                ]
            }, win);
        },

        initTable: function () {//回访评价
            layui.use(['table'], function () {
                table = layui.table;
                gwai740EditTable = table.render({
                    elem: app.getByid(doc, 'gwai740_huifang_table'),
                    id: 'gwai740_huifang_table',
                    cols: cols,
                    height:'150',
                    page: false
                });

                table.on('edit(gwai740_huifang_table)', function (obj) {
                    if (obj.field == 'MMRN004') {
                        var re = /^[0-9]+.?[0-9]*$/;//判断是否为数字
                        if((!re.test(obj.value)) || parseFloat(obj.value) < 1 || parseFloat(obj.value) > 5){
                            obj.updateOfEdit({
                                'MMRN004': ''
                            });
                            app.showWarning("得分为1-5的数字！");
                            return ;
                        }
                    }
                });

            });
        },

        tableReload: function (listData) {
            gwai740EditTable = table.reload('gwai740_huifang_table', {
                data: listData
            });
        },

        editData: function (operation,list) {
            app.ajax({
                xurl: 'crm/gwai740/edit',
                xdata: {operation:operation,list:list},
                loading: true,
                toJsonStr: true,
                okmsg: "回访结案成功",
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwai740').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            vueData.templateImageUrl = [];
            callObj = tagData.obj;
            app.extendUpLow({
                target: vueData.form,
                res: callObj,
                reset: true
            });
            if(callObj.MMRG100 == '1'){
                vueData.form.MMRG100_DATA = "处理完成";
            }else if(callObj.MMRG100 == '2'){
                vueData.form.MMRG100_DATA = "暂缓处理";
            }else if(callObj.MMRG100 == '3'){
                vueData.form.MMRG100_DATA = "未解决问题";
            }else {
                vueData.form.MMRG100_DATA = "";
            }
            if(callObj.MMRD013 != undefined && callObj.MMRD013 != ''){
                vueData.form.MMRD013S = callObj.MMRD013.split(',');
            }else {
                vueData.form.MMRD013S = [];
            }
            if(tagData.is_detail){
                vueData.isDetail = true;
                app.ajax({
                    xurl: 'crm/gwai740/getMMRN',
                    xdata: {MMRN001:callObj.MMRDDOCNO,MMRN002:callObj.MMRDSEQ},
                    cb: function (result) {
                        if (result.ok) {
                            if(result.data.data != null && result.data.data.length > 0){
                                vueData.form.MMRNDATE = result.data.data[0].MMRNDATE != undefined ? result.data.data[0].MMRNDATE : '';
                                vueData.form.MMRN008 = result.data.data[0].MMRN008;
                                vueData.form.MMRN005 = result.data.data[0].MMRN005;
                                self.tableReload(result.data.data);
                            }
                        }
                    }
                });
            }else {
                vueData.isDetail = false;
                self.getTable();
            }
            if(callObj.MMRD015 != undefined && callObj.MMRD015.length > 0) {
                var list_mmrs015 = callObj.MMRD015.split(',');
                list_mmrs015.forEach(function (v) {
                    vueData.templateImageUrl.push('/wissun-smfile/' + v);
                });
            }
            app.ajax({
                xurl: 'crm/gwai740/getMMRF',
                xdata: {MMRF001:callObj.MMRDDOCNO,MMRF002:callObj.MMRDSEQ},
                cb: function (result) {
                    if (result.ok) {
                        vueData.form.MMRG102 = result.data.data.MMRG102;
                        vueData.form.MMRG100 = result.data.data.MMRG100;
                    }
                }
            });
            app.ajax({
                xurl: 'crm/gwai740/getMmrq001',
                xdata: {MMRDDOCNO:callObj.MMRDDOCNO,MMRDSEQ:callObj.MMRDSEQ},
                cb: function (result) {
                    if (result.ok) {
                        var str = [];
                        if(result.data.data.length > 0){
                            for(var i=0;i<result.data.data.length;i++){
                                str.push(result.data.data[i].MMRQ001);
                            }
                        }
                        vueData.form.MMRD013S = str;
                    }
                }
            });
            self.getMmrd013(callObj.MMRD002);

        },

        getMmrd013:function (MMRD002) {
            app.ajax({
                xurl: 'crm/gwai740/getMmrd013List',
                xdata: {MMRD002: MMRD002},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.mmrd013Options = result.data.data;
                    }
                }
            });
        },

        getTable:function () {
            app.ajax({
                xurl: 'crm/gwai740/getStarList',
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var list = result.data.data;
                        self.tableReload(list);
                    }
                }
            });
        },

        _ready: function () {
            table = layui.table;
            vueData.form = app.fields_(['MMRD_T'],vueData.form);
            var vue = new Vue({
                el: '#gwai740_huifang',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                vueData.form.operation = 'huifang';
                                var tableData = gwai740EditTable.getDatas();
                                var listData = [];
                                for(var i = 0;i<tableData.length;i++){
                                    listData.push({
                                        MMRN003:tableData[i]["MMRJ001"],
                                        MMRN004:tableData[i]["MMRN004"]
                                    });
                                }
                                listData.addMap(vueData.form, false);
                                self.editData(vueData.form.operation,listData);
                            }
                        });
                    },
                    openMmrd:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    columnList: [
                                        {label: '预约服务单号', prop: 'MMRDDOCNO' , width: '150'},
                                        {label: '单据日期', prop: 'MMRDDOCDATE', width: '150'},
                                        {label: '任务类型', prop: 'MMRD001_NAME', width: '150'},
                                        {label: '设备编号', prop: 'MMRD002', width: '150'},
                                        {label: '名称', prop: 'MMRA004', width: '150'},
                                        {label: '规格', prop: 'MMRA006', width: '150'},
                                        {label: '序列号', prop: 'MMRD003', width: '150'}
                                    ],
                                    url: 'crm/gwai740/getMmrddocnoPage',
                                    searchParam:{
                                        MMRD020:'5',//回访
                                    }
                                },
                                cb: function (data) {
                                    vueData.templateImageUrl = [];
                                    self.getMmrd013(data.MMRD002);
                                    app.extendUpLow({
                                        target: vueData.form,
                                        res: [data,{MMRD013S:data.MMRD013.split(',')}],
                                        reset: true
                                    });
                                    if(data.MMRG100 == '1'){
                                        vueData.form.MMRG100_DATA = "处理完成";
                                    }else if(data.MMRG100 == '2'){
                                        vueData.form.MMRG100_DATA = "暂缓处理";
                                    }else if(data.MMRG100 == '3'){
                                        vueData.form.MMRG100_DATA = "未解决问题";
                                    }else {
                                        vueData.form.MMRG100_DATA = "";
                                    }
                                    if(data.MMRD015 != undefined && data.MMRD015.length > 0) {
                                        var list_mmrs015 = data.MMRD015.split(',');
                                        list_mmrs015.forEach(function (v) {
                                            vueData.templateImageUrl.push('/wissun-smfile/' + v);
                                        });
                                    }
                                    app.ajax({
                                        xurl: 'crm/gwai740/getMMRF',
                                        xdata: {MMRF001:data.MMRDDOCNO,MMRF002:data.MMRDSEQ},
                                        cb: function (result) {
                                            if (result.ok) {
                                                vueData.form.MMRG102 = result.data.data.MMRG102;
                                                vueData.form.MMRG100 = result.data.data.MMRG100;
                                            }
                                        }
                                    });
                                    app.ajax({
                                        xurl: 'crm/gwai740/getMMRF',
                                        xdata: {MMRF001:data.MMRDDOCNO,MMRF002:data.MMRDSEQ},
                                        cb: function (result) {
                                            if (result.ok) {
                                                vueData.form.MMRG102 = result.data.data.MMRG102;
                                                vueData.form.MMRG100 = result.data.data.MMRG100;
                                            }
                                        }
                                    });
                                    self.getTable();
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择服务预约单',
                                area: ['1000px', '470px']
                            }
                        });
                    }

                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai740_huifang');
                }
            });
            self.initTable();
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwai740Edit();

});